import java.util.HashMap;

class Solution {
    /*
    单指针+哈希
    */
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> hm = new HashMap<>();
        int start = 0;
        int res = 0;
        char c;
        for (int i = 0; i<s.length();i++) {
            c = s.charAt(i);
            if (hm.get(c)!=null && hm.get(c)>=start){
                start = hm.get(c)+1;
            }
            hm.put(c,i);
            if (i-start+1 > res)
                res = i-start+1;
        }
        return res;
    }

    public static int maxLength(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        int slow = -1;
        int res = 0;
        for (int fast = 0; fast < s.length(); fast++) {
            char ch = s.charAt(fast);
            if (map.containsKey(ch)) {
                /*while (slow < map.get(ch)) {
                    slow++;
                }*/
                if (slow < map.get(ch)){
                    slow = map.get(ch);
                }
            }
            map.put(ch, fast);
            res = Math.max(res, fast - slow);
        }
        return res;
    }
}
